//
//  GetDataHandle.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/12.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "GetDataHandle.h"

@implementation GetDataHandle
+ (instancetype)sharedDataHandle{
    static GetDataHandle *getDataHandle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        getDataHandle = [GetDataHandle new];
    });
    return getDataHandle;
}


- (void)getDataWithUrlString:(NSString *)urlStr withGetDataBlock:(GetDataBlock)getDataBlock{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask  *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"请求失败== %@",error);
        }else{
            NSLog(@"请求成功");
//            回到主线程（回调Block）
            dispatch_async(dispatch_get_main_queue(), ^{
                getDataBlock(data);
            });
        }
    }];
    [task resume];
}

@end
